/*
 * Day.java
 * 
 * Copyright (c) 2008, Marshall Scorcio, Ralph Holzmann, 
 *                     Stewart Sweet, Tyler Hoersch, Jeffery Van Handel
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the University of Wisconsin Oshkosh nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY COPYRIGHT HOLDERS AND CONTRIBUTORS ''AS IS'' AND 
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 */
import java.util.Vector;

public class Day implements java.io.Serializable
{
    public static final int SUNDAY = 0;
    public static final int MONDAY = 1;
    public static final int TUESDAY = 2;
    public static final int WEDNESDAY = 3;
    public static final int THURSDAY = 4;
    public static final int FRIDAY = 5;
    public static final int SATURDAY = 6;
    
    public static final int JANUARY = 0;
    public static final int FEBRUARY = 1;
    public static final int MARCH = 2;
    public static final int APRIL = 3;
    public static final int MAY = 4;
    public static final int JUNE = 5;
    public static final int JULY = 6;
    public static final int AUGUST = 7;
    public static final int SEPTEMBER = 8;
    public static final int OCTOBER = 9;
    public static final int NOVEMBER = 10;
    public static final int DECEMBER = 11;     

    private int dayOfWeek;
    private int dayOfMonth;
    private int month;
    private int year;
    
    private Vector<TimeBlock> times = new Vector<TimeBlock>();
	
    public Day(int dayOfWeek, int dayOfMonth, int month, int year)
    {
        this(dayOfWeek);
        setDate(dayOfMonth, month, year);
    }
    
    public Day(int dayOfWeek)
    {
        dayOfMonth = -1;
        month = -1;
        year = -1;
        this.dayOfWeek = dayOfWeek;
    }
    
    public Day(String day)
    {   
        if (day.indexOf(",") != -1)
        {
            dayOfWeek = getDayOfWeekFromName(day.substring(0, day.indexOf(",")));
            day = day.substring(day.indexOf(",") + 2);
            
            month = getMonthFromName(day.substring(0, day.indexOf(" ")));
            day = day.substring(day.indexOf(" ") + 1);
            
            dayOfMonth = Integer.parseInt(day.substring(0, day.indexOf(", ")));
            day = day.substring(day.indexOf(",") + 2);
                    
            year = Integer.parseInt(day);
        }
        else
        {
            dayOfWeek = getDayOfWeekFromName(day);
            dayOfMonth = -1;
            month = -1;
            year = -1;
        }
    }
    
    @Override
    public boolean equals(Object obj)
    {
        boolean equal = false;
        
        if (this == obj)
        {
            equal = true;
        }
        else if (obj instanceof Day)
        {
            Day day = (Day) obj;
            if ((this.dayOfMonth == day.getDayOfMonth()) && (this.month == day.getMonth()) && (this.year == day.getYear()))
            {
               equal = true;
            }
        }
        
        return equal;
    }
    
    public String getName()
    {
        return getNameFromDayOfWeek(dayOfWeek);
    }
    
    public int getDayOfWeek() 
    {
        return dayOfWeek;
    }
    
    public int getDayOfMonth()
    {
        return dayOfMonth;
    }
    
    public int getMonth()
    {
        return month;
    }
    
    public int getYear()
    {
        return year;
    }

    public static String getNameFromDayOfWeek(int dayOfWeek)
    {
        String name = "";
        
        switch(dayOfWeek)
        {
            case SUNDAY: 
                name = "Sunday";
                break;
            case MONDAY:
                name = "Monday";
                break;
            case TUESDAY:
                name = "Tuesday";
                break;
            case WEDNESDAY:
                name = "Wednesday";
                break;
            case THURSDAY:
                name = "Thursday";
                break;
            case FRIDAY:
                name = "Friday";
                break;
            case SATURDAY:
                name = "Saturday";
                break;
        }
        
        return name;
    }
    
    public static int getDayOfWeekFromName(String name)
    {
        int dayOfWeek = -1;
        
        if (name.equals("Sunday"))
        {
            dayOfWeek = SUNDAY;
        }
        else if (name.equals("Monday"))
        {
            dayOfWeek = MONDAY;
        }
        else if (name.equals("Tuesday"))
        {
            dayOfWeek = TUESDAY;
        }
        else if (name.equals("Wednesday"))
        {
            dayOfWeek = WEDNESDAY;
        }
        else if (name.equals("Thursday"))
        {
            dayOfWeek = THURSDAY;
        }
        else if (name.equals("Friday"))
        {
            dayOfWeek = FRIDAY;
        }
        else if (name.equals("Saturday"))
        {
            dayOfWeek = SATURDAY;
        }
                
        return dayOfWeek;
    }
    
    public static int getMonthFromName(String name)
    {
        int month = -1;
        
        if (name.equals("January"))
        {
            month = JANUARY;
        }
        else if (name.equals("February"))
        {
            month = FEBRUARY;
        }
        else if (name.equals("March"))
        {
            month = MARCH;
        }
        else if (name.equals("April"))
        {
            month = APRIL;
        }
        else if (name.equals("May"))
        {
            month = MAY;
        }
        else if (name.equals("June"))
        {
            month = JUNE;
        }
        else if (name.equals("July"))
        {
            month = JULY;
        }
        else if (name.equals("August"))
        {
            month = AUGUST;
        }        
        else if (name.equals("September"))
        {
            month = SEPTEMBER;
        }        
        else if (name.equals("October"))
        {
            month = OCTOBER;
        }        
        else if (name.equals("November"))
        {
            month = NOVEMBER;
        }        
        else if (name.equals("December"))
        {
            month = DECEMBER;
        }        
        
        return month;
    }
    
    public static String getNameFromMonth(int month)
    {                           
        String name = "";
        
        switch(month)
        {
            case JANUARY: 
                name = "January";
                break;
            case FEBRUARY:
                name = "February";
                break;
            case MARCH:
                name = "March";
                break;
            case APRIL:
                name = "April";
                break;
            case MAY:
                name = "May";
                break;
            case JUNE:
                name = "June";
                break;
            case JULY:
                name = "July";
                break;
            case AUGUST:
                name = "August";
                break;
            case SEPTEMBER:
                name = "September";
                break;
            case OCTOBER:
                name = "October";
                break;
            case NOVEMBER:
                name = "November";
                break;
            case DECEMBER:
                name = "December";
                break;
        }
        
        return name;
    }
    
    public void setDate(int dayOfMonth, int month, int year)
    {
        this.dayOfMonth = dayOfMonth;
        this.month = month;
        this.year = year;
    }
    
    public boolean hasDate()
    {
        return !(month == -1 || dayOfMonth == -1 || year == -1);
    }
	
    @Override
    public String toString()
    {
        String formatted = "";
        
        if (hasDate())
        {
            formatted = getName() + ", " + getNameFromMonth(month) + " " + dayOfMonth + ", " + year;
        }
        else
        {
            formatted = getName();
        } 
        
        return formatted;
    }
    
    public boolean hasTimeBlocks()
    {
        boolean timeBlocks = false;
        
        if (getNumberOfTimeBlocks() != 0)
        {
            timeBlocks = true;
        }
        
        return timeBlocks;
    }
    
    public void addTimeBlock(int startHour, int startMinute, int endHour, int endMinute)
    {   
        times.add(new TimeBlock(startHour, startMinute, endHour, endMinute));
    }

    public void addTimeBlock(TimeBlock block)
    {   
        times.add(block);
    }
    
    public void addTimeBlock(String block)
    {
        times.add(new TimeBlock(block));
    }
    
    public void deleteTimeBlock(int startHour, int startMinute, int endHour, int endMinute)
    {        
        TimeBlock toDelete = new TimeBlock(startHour, startMinute, endHour, endMinute);
        
        if (times.contains(toDelete))
        {
            times.remove(toDelete);
        }
    }
    
    public Vector<TimeBlock> getTimeBlocks()
    {
        return times;
    }
    
    public void deleteAllTimeBlocks()
    {
        times.clear();
    }
    
    public int getNumberOfTimeBlocks()
    {
        return times.size();
    }
}
